//
//  Login.m
//  EquiriesProject
//
//  Created by qianfeng on 15/10/31.
//  Copyright (c) 2015年 HXH. All rights reserved.
//

#import "Login.h"

@interface Login ()<UIWebViewDelegate>

@property(nonatomic,strong)UIWebView * webView;
@end

@implementation Login

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登录";
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self creatNavBar];
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    
    self.webView.delegate = self;
    
    [self.view addSubview:self.webView];
    
    //https://api.weibo.com/oauth2/authorize?client_id=3601604349&redirect_uri=http://hi.baidu.com/jt_one&display=mobile&response_type=token
    NSDictionary * dic = @{@"client_id":SINA_APP_KEY,@"redirect_uri":redirect_uri,@"display":@"mobile",@"response_type":@"token"};
    
    NSString * loginPath = [Help pathWithBasicPath:SINA_AUTHORIZE_SEVER andDic:dic];
    
    NSURL * url = [NSURL URLWithString:loginPath];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    
    //通过webView加载
    [self.webView loadRequest:request];
}

-(void)creatNavBar
{
    UIButton * left=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    
    [left setBackgroundImage:[UIImage imageNamed:@"back_normal"] forState:UIControlStateNormal];
    [left addTarget:self action:@selector(leftButton) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftItem=[[UIBarButtonItem alloc]initWithCustomView:left];
    self.navigationItem.leftBarButtonItem=leftItem;
}

-(void)leftButton
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


#pragma mark --UIWebViewDelegate
//将要开始请求时回调
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //显示指示器
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSArray * arr = [request.URL.absoluteString componentsSeparatedByString:@"#"];
    
    //添加指示器
    if (arr.count == 2) {
        NSLog(@"arr[1] == %@",arr[1]);
        
        NSString * infoStr = arr[1];
        
        NSString * access_token = [Help getValueWithInfo:infoStr andKey:@"access_token="];
        NSString * remind_in = [Help getValueWithInfo:infoStr andKey:@"remind_in="];
        NSString * expires_in = [Help getValueWithInfo:infoStr andKey:@"expires_in="];
        NSString * uid = [Help getValueWithInfo:infoStr andKey:@"uid="];
        [[NSUserDefaults standardUserDefaults] setObject:access_token forKey:ACCESS_TOKEN];
        [[NSUserDefaults standardUserDefaults] setObject:uid forKey:UID];
        //[[NSUserDefaults standardUserDefaults] setObject:access_token forKey:ACCESS_TOKEN];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        if (expires_in != nil) {
            NSTimeInterval time = [expires_in longLongValue];
            NSDate * futherDate = [NSDate dateWithTimeIntervalSinceNow:time];
            
            [[NSUserDefaults standardUserDefaults] setObject:futherDate forKey:EXPIRES_IN];
            [[NSUserDefaults standardUserDefaults]synchronize];
            
            //NSLog(@"futherDate == %@",futherDate);
        }
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
        if ([self.delegate respondsToSelector:@selector(pushView)]) {
            
            [self.delegate pushView];
            
        }

        
        return NO;
    }
    
    //每一次将要请求时的接口
    NSLog(@"%@",request.URL.absoluteString);
    //NSLog(@"%s",__func__);
    //停止指示器
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    
    return YES;
}
//开始请求时回调
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    //NSLog(@"%s",__func__);
}
//请求完成时回调
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //NSLog(@"%s",__func__);
}
//请求失败时回调
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    //NSLog(@"%s",__func__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
